<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>排片管理 - 经理仪表盘</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
            line-height: 1.6;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .nav {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
        }
        
        .logo {
            font-size: 24px;
            font-weight: bold;
        }
        
        .nav-links {
            display: flex;
            gap: 30px;
        }
        
        .nav-links a {
            color: white;
            text-decoration: none;
            font-weight: 500;
            transition: opacity 0.3s;
        }
        
        .nav-links a:hover {
            opacity: 0.8;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .section-title {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }
        
        .card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            margin-bottom: 30px;
            overflow: hidden;
        }
        
        .card-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            font-size: 18px;
            font-weight: bold;
        }
        
        .card-body {
            padding: 30px;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-weight: 500;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
            color: white;
            text-decoration: none;
        }
        
        .btn-sm {
            padding: 8px 16px;
            font-size: 14px;
        }
        
        .btn-success {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
        }
        
        .btn-danger {
            background: linear-gradient(135deg, #dc3545 0%, #fd7e14 100%);
        }
        
        .btn-warning {
            background: linear-gradient(135deg, #ffc107 0%, #fd7e14 100%);
            color: #000;
        }
        
        .btn-secondary {
            background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
        }
        
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        .form-control:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        .table th,
        .table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #e1e1e1;
        }
        
        .table th {
            background-color: #f8f9fa;
            font-weight: bold;
            color: #333;
        }
        
        .table tr:hover {
            background-color: #f8f9fa;
        }
        
        .alert {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .badge {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .bg-success {
            background-color: #28a745;
            color: white;
        }
        
        .bg-warning {
            background-color: #ffc107;
            color: #000;
        }
        
        .bg-danger {
            background-color: #dc3545;
            color: white;
        }
        
        .bg-secondary {
            background-color: #6c757d;
            color: white;
        }
        
        .bg-primary {
            background-color: #667eea;
            color: white;
        }
        
        .text-center {
            text-align: center;
        }
        
        .text-muted {
            color: #666;
        }
        
        .grid-2 {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }
        
        .grid-3 {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
        }
        
        .grid-4 {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 20px;
        }
        
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        
        .modal-dialog {
            position: relative;
            margin: 50px auto;
            max-width: 600px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
        }
        
        .modal-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            border-radius: 15px 15px 0 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .modal-body {
            padding: 30px;
        }
        
        .modal-footer {
            padding: 20px;
            border-top: 1px solid #e1e1e1;
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }
        
        .close {
            background: none;
            border: none;
            color: white;
            font-size: 24px;
            cursor: pointer;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }
        
        .form-select {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            background-color: white;
        }
        
        @media (max-width: 768px) {
            .grid-2, .grid-3, .grid-4 {
                grid-template-columns: 1fr;
            }
            
            .nav {
                flex-direction: column;
                gap: 15px;
            }
            
            .nav-links {
                gap: 15px;
            }
        }
    </style>
</head>
<body>
    <header class="header">
        <nav class="nav">
            <div class="logo">🎬 经理控制台</div>
            <div class="nav-links">
                <a href="/manager/dashboard">控制台</a>
                <a href="/manager/password-management">密码管理</a>
                <a href="/manager/movies">影片管理</a>
                <a href="/manager/screenings">排片管理</a>
                <a href="/manager/sales-data">销售数据</a>
            </div>
            <div class="user-info">
                <span th:text="|经理：${session.user != null ? session.user.username : '未登录'}|">经理</span>
                <a href="/auth/logout" class="btn btn-sm">退出登录</a>
            </div>
        </nav>
    </header>

    <div class="container">
        <h2 class="section-title">📅 排片管理</h2>
        
        <!-- 消息提示 -->
        <div th:if="${message}" class="alert alert-success">
            ✅ <span th:text="${message}"></span>
        </div>
        
        <div th:if="${error}" class="alert alert-danger">
            ❌ <span th:text="${error}"></span>
        </div>

        <!-- 筛选和操作 -->
        <div class="grid-2">
            <div class="card">
                <div class="card-header">
                    🔍 筛选场次
                </div>
                <div class="card-body">
                    <form action="/manager/screenings" method="get" style="display: flex; gap: 10px; align-items: center;">
                        <select class="form-control" name="period">
                            <option value="today" th:selected="${period == 'today'}">今日</option>
                            <option value="3days" th:selected="${period == '3days'}">近3日</option>
                            <option value="week" th:selected="${period == 'week'}">近一周</option>
                        </select>
                        <button type="submit" class="btn">
                            🔍 筛选
                        </button>
                    </form>
                </div>
            </div>
            <div class="card">
                <div class="card-header">
                    ⚡ 快速操作
                </div>
                <div class="card-body text-center">
                    <button type="button" class="btn btn-success" onclick="openAddScreeningModal()" style="margin-right: 10px;">
                        ➕ 增加场次
                    </button>
                    <button type="button" class="btn btn-warning" onclick="autoSchedule()">
                        🪄 自动排片
                    </button>
                </div>
            </div>
        </div>

        <!-- 场次列表 -->
        <div class="card">
            <div class="card-header">
                📋 场次列表 <span class="badge bg-primary" th:text="${#lists.size(screenings)}">0</span>
            </div>
            <div class="card-body">
                <div style="overflow-x: auto;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>场次ID</th>
                                <th>影片</th>
                                <th>放映厅</th>
                                <th>放映时间</th>
                                <th>票价</th>
                                <th>座位状态</th>
                                <th>售票状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr th:each="screening : ${screenings}">
                                <td th:text="${screening.id}"></td>
                                <td>
                                    <strong th:text="${screening.movie.title}"></strong>
                                    <br>
                                    <small class="text-muted" th:text="${screening.movie.duration} + '分钟'"></small>
                                </td>
                                <td th:text="${screening.room.name}"></td>
                                <td>
                                    <span th:text="${#temporals.format(screening.startTime, 'MM-dd HH:mm')}"></span>
                                    <br>
                                    <small class="text-muted" th:text="${#temporals.format(screening.startTime, 'EEEE')}"></small>
                                </td>
                                <td>
                                    <span class="badge bg-success" th:text="|¥${screening.price}|"></span>
                                </td>
                                <td>
                                    <div style="background-color: #e9ecef; border-radius: 10px; height: 20px; position: relative; overflow: hidden;">
                                        <div style="background: linear-gradient(135deg, #28a745 0%, #20c997 100%); height: 100%; border-radius: 10px; transition: width: 0.3s;" 
                                             th:style="|width: ${#lists.size(screening.orders) * 100 / screening.room.totalSeats}%|"></div>
                                        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 12px; font-weight: bold; color: #333;"
                                             th:text="|${screening.room.totalSeats - #lists.size(screening.orders)}/${screening.room.totalSeats}|"></div>
                                    </div>
                                </td>
                                <td>
                                    <span th:if="${#lists.size(screening.orders) == 0}" class="badge bg-success">✅ 未售票</span>
                                    <span th:if="${#lists.size(screening.orders) > 0 && #lists.size(screening.orders) < screening.room.totalSeats}" class="badge bg-warning">⏰ 部分售出</span>
                                    <span th:if="${#lists.size(screening.orders) == screening.room.totalSeats}" class="badge bg-danger">❌ 已售罄</span>
                                </td>
                                <td>
                                    <button type="button" class="btn btn-sm" 
                                            onclick="editScreening(this)" 
                                            th:data-screening-id="${screening.id}"
                th:data-movie-id="${screening.movie.id}"
                                            th:data-room-id="${screening.room.id}"
                                            th:data-time="${#temporals.format(screening.startTime, 'yyyy-MM-dd HH:mm')}"
                                            th:data-price="${screening.price}"
                                            th:data-has-tickets="${#lists.size(screening.orders) > 0}" style="margin-right: 5px;">
                                        ✏️ 编辑
                                    </button>
                                    <form action="/manager/screenings/delete" method="post" style="display: inline;" 
                                          onsubmit="return confirmDelete(this)">
                                        <input type="hidden" name="screeningId" th:value="${screening.id}">
                                        <input type="hidden" name="period" th:value="${period}">
                                        <input type="hidden" name="movieTitle" th:value="${screening.movie.title}">
                                        <input type="hidden" name="screeningTime" th:value="${#temporals.format(screening.startTime, 'MM-dd HH:mm')}">
                                        <input type="hidden" name="hasTickets" th:value="${#lists.size(screening.orders) > 0}">
                                        <button type="submit" class="btn btn-sm btn-danger">
                                            🗑️ 删除
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                
                <div th:if="${#lists.isEmpty(screenings)}" class="text-center" style="padding: 40px 0;">
                    <div style="font-size: 48px; margin-bottom: 20px;">📅</div>
                    <p class="text-muted">暂无场次数据</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 添加场次模态框 -->
    <div class="modal" id="addScreeningModal">
        <div class="modal-dialog">
            <div class="modal-header">
                <h3>➕ 添加场次</h3>
                <button type="button" class="close" onclick="closeModal('addScreeningModal')">&times;</button>
            </div>
            <form action="/manager/screenings/add" method="post">
                <input type="hidden" name="period" th:value="${period}">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="addMovieId" class="form-label">选择影片 *</label>
                        <select class="form-select" id="addMovieId" name="movieId" required>
                            <option value="">请选择影片</option>
                            <option th:each="movie : ${movies}" 
                                    th:value="${movie.id}" 
                                    th:text="|${movie.title} (${movie.duration}分钟)|">
                            </option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="addRoomId" class="form-label">选择放映厅 *</label>
                        <select class="form-select" id="addRoomId" name="roomId" required>
                            <option value="">请选择放映厅</option>
                            <option th:each="room : ${rooms}" 
                                    th:value="${room.id}" 
                                    th:text="|${room.name} (容量:${room.totalSeats})|">
                            </option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="addScreeningTime" class="form-label">放映时间 *</label>
                        <input type="datetime-local" class="form-control" id="addScreeningTime" name="startTime" required>
                        <small class="text-muted">允许提前安排一周的电影场次</small>
                    </div>
                    <div class="form-group">
                        <label for="addPrice" class="form-label">票价 *</label>
                        <div style="display: flex; align-items: center;">
                            <span style="padding: 12px; background-color: #f8f9fa; border: 1px solid #ddd; border-right: none; border-radius: 8px 0 0 8px;">¥</span>
                            <input type="number" class="form-control" id="addPrice" name="price" min="0" step="0.01" required style="border-radius: 0 8px 8px 0;">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" onclick="closeModal('addScreeningModal')">取消</button>
                    <button type="submit" class="btn btn-success">
                        💾 添加场次
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- 编辑场次模态框 -->
    <div class="modal" id="editScreeningModal">
        <div class="modal-dialog">
            <div class="modal-header">
                <h3>✏️ 编辑场次</h3>
                <button type="button" class="close" onclick="closeModal('editScreeningModal')">&times;</button>
            </div>
            <form action="/manager/screenings/edit" method="post">
                <input type="hidden" id="editScreeningId" name="screeningId">
                <input type="hidden" name="period" th:value="${period}">
                <div class="modal-body">
                    <div id="editWarning" class="alert alert-warning" style="display: none;">
                        ⚠️ <strong>注意：</strong>该场次已有售出的电影票，修改可能影响已购票用户。
                    </div>
                    <div class="form-group">
                        <label for="editMovieId" class="form-label">选择影片 *</label>
                        <select class="form-select" id="editMovieId" name="movieId" required>
                            <option value="">请选择影片</option>
                            <option th:each="movie : ${movies}" 
                                    th:value="${movie.id}" 
                                    th:text="|${movie.title} (${movie.duration}分钟)|">
                            </option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="editRoomId" class="form-label">选择放映厅 *</label>
                        <select class="form-select" id="editRoomId" name="roomId" required>
                            <option value="">请选择放映厅</option>
                            <option th:each="room : ${rooms}" 
                                    th:value="${room.id}" 
                                    th:text="|${room.name} (容量:${room.totalSeats})|">
                            </option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="editScreeningTime" class="form-label">放映时间 *</label>
                        <input type="datetime-local" class="form-control" id="editScreeningTime" name="startTime" required>
                    </div>
                    <div class="form-group">
                        <label for="editPrice" class="form-label">票价 *</label>
                        <div style="display: flex; align-items: center;">
                            <span style="padding: 12px; background-color: #f8f9fa; border: 1px solid #ddd; border-right: none; border-radius: 8px 0 0 8px;">¥</span>
                            <input type="number" class="form-control" id="editPrice" name="price" min="0" step="0.01" required style="border-radius: 0 8px 8px 0;">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" onclick="closeModal('editScreeningModal')">取消</button>
                    <button type="submit" class="btn btn-primary">
                        💾 保存修改
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 设置最小时间为当前时间
        const now = new Date();
        const minDateTime = now.toISOString().slice(0, 16);
        document.getElementById('addScreeningTime').min = minDateTime;
        document.getElementById('editScreeningTime').min = minDateTime;
        
        // 设置最大时间为一周后
        const maxDate = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
        const maxDateTime = maxDate.toISOString().slice(0, 16);
        document.getElementById('addScreeningTime').max = maxDateTime;
        document.getElementById('editScreeningTime').max = maxDateTime;
        
        function openAddScreeningModal() {
            document.getElementById('addScreeningModal').style.display = 'block';
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).style.display = 'none';
        }
        
        // 点击模态框外部关闭
        window.onclick = function(event) {
            const addModal = document.getElementById('addScreeningModal');
            const editModal = document.getElementById('editScreeningModal');
            if (event.target == addModal) {
                addModal.style.display = 'none';
            }
            if (event.target == editModal) {
                editModal.style.display = 'none';
            }
        }
        
        function editScreening(button) {
            const screeningId = button.getAttribute('data-screening-id');
            const movieId = button.getAttribute('data-movie-id');
            const roomId = button.getAttribute('data-room-id');
            const time = button.getAttribute('data-time');
            const price = button.getAttribute('data-price');
            const hasTickets = button.getAttribute('data-has-tickets') === 'true';
            
            document.getElementById('editScreeningId').value = screeningId;
            document.getElementById('editMovieId').value = movieId;
            document.getElementById('editRoomId').value = roomId;
            document.getElementById('editScreeningTime').value = time;
            document.getElementById('editPrice').value = price;
            
            // 显示警告信息
            const warning = document.getElementById('editWarning');
            if (hasTickets) {
                warning.style.display = 'block';
            } else {
                warning.style.display = 'none';
            }
            
            document.getElementById('editScreeningModal').style.display = 'block';
        }
        
        function confirmDelete(form) {
            const movieTitle = form.movieTitle.value;
            const screeningTime = form.screeningTime.value;
            const hasTickets = form.hasTickets.value === 'true';
            
            if (hasTickets) {
                alert('该场次已有售出的电影票，不允许删除！');
                return false;
            }
            
            return confirm('确定要删除场次吗？\n\n影片：' + movieTitle + '\n时间：' + screeningTime + '\n\n删除后无法恢复！');
        }
        
        function autoSchedule() {
            if (confirm('确定要执行自动排片吗？这将为所有影片在未来一周内自动安排场次。')) {
                fetch('/manager/screenings/auto-schedule', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('自动排片成功！' + data.message);
                        location.reload();
                    } else {
                        alert('自动排片失败：' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('自动排片失败，请稍后重试。');
                });
            }
        }
    </script>
</body>
</html>